/**
 * 
 */
package habitcoach.ui;

import habitcoach.controller.DefaultHabitManager;
import habitcoach.controller.HabitManager;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author Coddy created on 2010-4-28
 * 
 */
public class Frame extends JFrame
{
	HabitManager manager;
	private JTextField tfText;
	private JTextArea taContent;
	private JButton button;

	public Frame() throws HeadlessException
	{
		super();
		manager = DefaultHabitManager.getInstance();

		initComponents();
	}

	/**
	 * 
	 */
	private void initComponents()
	{
		tfText = new JTextField();
		taContent = new JTextArea();
		button = new JButton("Add");

		taContent.setEditable(false);
		taContent.setBackground(Color.LIGHT_GRAY);

		button.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				addHabit();
			}
		});
		tfText.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				addHabit();
			}

		});

		this.setBackground(Color.LIGHT_GRAY);
		this.setVisible(true);
		this.setSize(400, 400);
		this.setLocation(200, 200);

		BorderLayout BL = new BorderLayout();
		this.setLayout(BL);
		this.add(taContent, BorderLayout.CENTER);

		JPanel panel = new JPanel();

		BorderLayout bl2 = new BorderLayout();
		panel.setLayout(bl2);
		panel.add(tfText, BorderLayout.CENTER);
		panel.add(button, BorderLayout.EAST);
		this.add(panel, BorderLayout.SOUTH);
		// this.pack();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	/**
	 * 
	 */
	protected void addHabit()
	{
		String str = tfText.getText().trim();
		if (null == str || str.equalsIgnoreCase(""))
		{
			JOptionPane.showMessageDialog(this, "Empty Input");
		} else
		{
			tfText.setText("");
			manager.addHabit(str);
		}

	}

	public static void main(String[] args)
	{
		Frame frame = new Frame();
	}
}
